<?php
	if((! isset($_REQUEST['xml'])) || ($_REQUEST['xml'] == '') || ($_REQUEST['xml'] != 1)){
		$xml = 0;
	}else{
		$xml = 1;
	}
	
	session_start();
	if(session_is_registered(username)){
		$name=$_SESSION['username'];
	}
	else{
		//header("refresh:3; url=../../home.html");
		//echo "You need to be logged in to access this page - redirecting in 3 seconds...";
		if($xml==0){
			$error['Result'] = array('DeleteTag'=>'notLoggedInError');
			echo json_encode($error);
			die;
		}else{
			$doc = new DomDocument('1.0');
			// create root node
			$root = $doc->createElement('Result');
			$root = $doc->appendChild($root);
			$attr = $doc->createAttribute("xmlns:xsi");
			$attr->value = "http://www.w3.org/2001/XMLSchema-instance";
			$root->appendChild($attr);
			$attr = $doc->createAttribute("xsi:noNamespaceSchemaLocation");
			$attr->value = "http://gnomo.fe.up.pt/~ei10048/trabalho2/xsd.xsd";
			$root->appendChild($attr);
			//childs of root
			$error = $doc->createElement('DeleteTag');
			$error= $root->appendChild($error);
			$value = $doc->createTextNode('notLoggedInError');
			$value = $error->appendChild($value);
			echo $doc->saveXML();
		}
		die;
		}

	
	if((! isset($_REQUEST['link'])) || ($_REQUEST['link'] == ''))
	{
		 if($xml==0){
			$error['Result'] = array('DeleteTag'=>'noLinkInParametersError');
			$doc->formatOutput = TRUE;
			echo json_encode($error);
			die;
		}else{
			$doc = new DomDocument('1.0');
			// create root node
			$root = $doc->createElement('Result');
			$root = $doc->appendChild($root);
			$attr = $doc->createAttribute("xmlns:xsi");
			$attr->value = "http://www.w3.org/2001/XMLSchema-instance";
			$root->appendChild($attr);
			$attr = $doc->createAttribute("xsi:noNamespaceSchemaLocation");
			$attr->value = "http://gnomo.fe.up.pt/~ei10048/trabalho2/xsd.xsd";
			$root->appendChild($attr);
			//childs of root
			$error = $doc->createElement('DeleteTag');
			$error= $root->appendChild($error);
			$value = $doc->createTextNode('noLinkInParametersError');
			$value = $error->appendChild($value);
			echo $doc->saveXML();
		}
		die;
	}
	else{$link = strtolower($_REQUEST['link']);}
	
	if((! isset($_REQUEST['tags'])) || ($_REQUEST['tags'] == ''))
	{
		 if($xml==0){
			$error['Result'] = array('DeleteTag'=>'noTagsInParametersError');
			$doc->formatOutput = TRUE;
			echo json_encode($error);
			die;
		}else{
			$doc = new DomDocument('1.0');
			// create root node
			$root = $doc->createElement('Result');
			$root = $doc->appendChild($root);
			$attr = $doc->createAttribute("xmlns:xsi");
			$attr->value = "http://www.w3.org/2001/XMLSchema-instance";
			$root->appendChild($attr);
			$attr = $doc->createAttribute("xsi:noNamespaceSchemaLocation");
			$attr->value = "http://gnomo.fe.up.pt/~ei10048/trabalho2/xsd.xsd";
			$root->appendChild($attr);
			//childs of root
			$error = $doc->createElement('DeleteTag');
			$error= $root->appendChild($error);
			$value = $doc->createTextNode('noTagsInParametersError');
			$value = $error->appendChild($value);
			echo $doc->saveXML();
		}
		die;
	}
	else{$tags = split(",", $_REQUEST['tags']);}
	
	try
	{
		$db = new PDO('sqlite:../../projecto1.db');
	}
	catch(PDOException $err)
	{
		if($xml==0){
			$error['Result'] = array('DeleteTag'=>'DatabaseError');
			echo json_encode($error);
		}else{
			$doc = new DomDocument('1.0');
			$doc->formatOutput = TRUE;
			// create root node
			$root = $doc->createElement('Result');
			$root = $doc->appendChild($root);
			$attr = $doc->createAttribute("xmlns:xsi");
			$attr->value = "http://www.w3.org/2001/XMLSchema-instance";
			$root->appendChild($attr);
			$attr = $doc->createAttribute("xsi:noNamespaceSchemaLocation");
			$attr->value = "http://gnomo.fe.up.pt/~ei10048/trabalho2/xsd.xsd";
			$root->appendChild($attr);
			//childs of root
			$error = $doc->createElement('DeleteTag');
			$error= $root->appendChild($error);
			$value = $doc->createTextNode('DatabaseError');
			$value = $error->appendChild($value);
			echo $doc->saveXML();
		}
		die;
	}
	
	$idLink = 0;
	$query = $db->prepare("SELECT * FROM Link WHERE linkURL = :link");
	$query->bindParam(":link", $link, PDO::PARAM_STR);
	$query->execute();
	$bd_link = $query->fetch();
	
	if(! $bd_link)
	{
		if($xml==0){
			$error['Result'] = array('DeleteTag'=>'LinkDoesntExistError');
			echo json_encode($error);
		}else{
			$doc = new DomDocument('1.0');
			$doc->formatOutput = TRUE;
			// create root node
			$root = $doc->createElement('Result');
			$root = $doc->appendChild($root);
			$attr = $doc->createAttribute("xmlns:xsi");
			$attr->value = "http://www.w3.org/2001/XMLSchema-instance";
			$root->appendChild($attr);
			$attr = $doc->createAttribute("xsi:noNamespaceSchemaLocation");
			$attr->value = "http://gnomo.fe.up.pt/~ei10048/trabalho2/xsd.xsd";
			$root->appendChild($attr);
			//childs of root
			$error = $doc->createElement('DeleteTag');
			$error= $root->appendChild($error);
			$value = $doc->createTextNode('LinkDoesntExistError');
			$value = $error->appendChild($value);
			echo $doc->saveXML();
		}
		die;
	}
	else
		$idLink = $bd_link['idLink'];
	
	$query = $db->prepare("SELECT * FROM User WHERE userName = :username");
	$query->bindParam(":username", $_SESSION['username'], PDO::PARAM_STR);
	$query->execute();
	$id_user = $query->fetch();
	$id_user = $id_user['idUser'];
	
	$query = $db->prepare("SELECT * FROM User_Link WHERE idUser = :idUser AND idLink = :idLink");
	$query->bindParam(":idUser", $id_user);
	$query->bindParam(":idLink", $idLink);
	$query->execute();
	$id_user_link = $query->fetch();
	
	if(! $id_user_link)
	{
		if($xml==0){
			$error['Result'] = array('DeleteTag'=>'UserDoesntHaveThatLinkError');
			echo json_encode($error);
		}else{
			$doc = new DomDocument('1.0');
			$doc->formatOutput = TRUE;
			// create root node
			$root = $doc->createElement('Result');
			$root = $doc->appendChild($root);
			$attr = $doc->createAttribute("xmlns:xsi");
			$attr->value = "http://www.w3.org/2001/XMLSchema-instance";
			$root->appendChild($attr);
			$attr = $doc->createAttribute("xsi:noNamespaceSchemaLocation");
			$attr->value = "http://gnomo.fe.up.pt/~ei10048/trabalho2/xsd.xsd";
			$root->appendChild($attr);
			//childs of root
			$error = $doc->createElement('DeleteTag');
			$error= $root->appendChild($error);
			$value = $doc->createTextNode('UserDoesntHaveThatLinkError');
			$value = $error->appendChild($value);
			echo $doc->saveXML();
		}
		die;
	}
	else
		$id_user_link = $id_user_link['idUserLink'];
		
	foreach($tags as $tag)
	{
		$tag = strtolower($tag);

		$query = $db->prepare("SELECT * FROM Tag WHERE tagName = :tag");
		$query->bindParam(":tag", $tag);
		$query->execute();
		$bd_tag = $query->fetch();
		
		if(! $bd_tag)
			continue;
		else
			$id_tag = $bd_tag['idTag'];
			
		$query = $db->prepare("DELETE FROM User_Link_Tag WHERE idUserLink = :idUserLink AND idTag = :idTag;");
		$query->bindParam(":idUserLink", $id_user_link);
		$query->bindParam(":idTag", $id_tag);
		$query->execute();
	}
	
		if($xml==0){
			$error['Result'] = array('DeleteTag'=>'Success');
			echo json_encode($error);
		}else{
			$doc = new DomDocument('1.0');
			$doc->formatOutput = TRUE;
			// create root node
			$root = $doc->createElement('Result');
			$root = $doc->appendChild($root);
			$attr = $doc->createAttribute("xmlns:xsi");
			$attr->value = "http://www.w3.org/2001/XMLSchema-instance";
			$root->appendChild($attr);
			$attr = $doc->createAttribute("xsi:noNamespaceSchemaLocation");
			$attr->value = "http://gnomo.fe.up.pt/~ei10048/trabalho2/xsd.xsd";
			$root->appendChild($attr);
			//childs of root
			$error = $doc->createElement('DeleteTag');
			$error= $root->appendChild($error);
			$value = $doc->createTextNode('Success');
			$value = $error->appendChild($value);
			echo $doc->saveXML();
		}
		die;
?>